# include "Enemy.h"

# include "Renderer.h"
# include "AI.h"
# include "GUI.h"

# include "World.h"
# include "Hero.h"
# include "Quests.h"


Enemy::Enemy() : Personage()
{

}

Enemy::~Enemy()
{

}

void Enemy::setLevel( unsigned int level)
{
	Personage::setLevel( level);

	lua_State* stateTmp = lua_open();
	luaL_openlibs( stateTmp);
	luaL_dofile( stateTmp, myLua.c_str());
	lua_settop( stateTmp, 0);

	lua_getglobal( stateTmp, "strength");
	lua_pushnumber( stateTmp, myLevel);
	lua_gettable( stateTmp, -2);
	myStrength = lua_tonumber( stateTmp, -1);
	lua_pop( stateTmp, 2);

	lua_getglobal( stateTmp, "stamina");
	lua_pushnumber( stateTmp, myLevel);
	lua_gettable( stateTmp, -2);
	myStamina = lua_tonumber( stateTmp, -1);
	lua_pop( stateTmp, 2);

	lua_close( stateTmp);

	myLife = myStamina * 10;
	myLifeCurrent = myLife;
}

void Enemy::setHighlight( bool highlight)
{
	myHighlight = highlight;
}

void Enemy::setAction( Action action)
{
	myAction = action;
}

void Enemy::setTarget( Hero* target)
{
	myTarget = target;
}

Ogre::Real Enemy::getDetectionDistance()
{
	return myDetectionDistance;
}

void Enemy::receiveDamage( const Damage& damage)
{
	Personage::receiveDamage( damage);

	if ( myLifeCurrent <= 0 )
	{
		Hero* heroTmp = myWorld->getHero();

		Experience expTmp;

		expTmp.experience = 100;
		expTmp.level = myLevel;
		heroTmp->receiveExperience( expTmp);
		
		if ( myQuest != NULL )
		{
			Quests* questsTmp = heroTmp->getQuests();
			
			questsTmp->nextStepQuest( myQuest->name);
		}

		if ( myHighlight )
		{
			CEGUI::ProgressBar* lifeTmp = (CEGUI::ProgressBar*) myGUI->findWindow( SHEET_BASE + SHEET_GAME + "Life");
			lifeTmp->setVisible( false);
		}
	}

	else if ( myHighlight )
	{
		CEGUI::ProgressBar* lifeTmp = (CEGUI::ProgressBar*) myGUI->findWindow( SHEET_BASE + SHEET_GAME + "Life");
		lifeTmp->setVisible( true);
		lifeTmp->setProgress( (float) myLifeCurrent/ (float) myLife);
	}	
}

void Enemy::init( World* world, Renderer* renderer, AI* ai, GUI* gui, const std::string& lua)
{
	Personage::init( renderer);

	myLua = lua;

	lua_State* stateTmp = lua_open();
	luaL_openlibs( stateTmp);
	luaL_dofile( stateTmp, myLua.c_str());

	std::string luaTmp;

	lua_settop( stateTmp, 0);
	lua_getglobal( stateTmp, "name");

	luaTmp = lua_tostring( stateTmp, 1);
	setName( luaTmp);
	lua_pop( stateTmp, 1);
	
	myWorld = world;
	myAI = ai;
	myGUI = gui;
	
	mySceneNode = renderer->createSceneNode( "EnemyNode");

	myBodyNode = mySceneNode->createChildSceneNode( "EnemyBodyNode");
	myBodyNode->showBoundingBox( true);

	lua_getglobal( stateTmp, "mesh");
	luaTmp = lua_tostring( stateTmp, 1);
	myBody = renderer->createEntity( "EnemyBody", luaTmp);
	lua_pop( stateTmp, 1);
	myBody->setQueryFlags( ENEMY_FLAG);
	myBodyNode->attachObject( myBody);

	lua_getglobal( stateTmp, "detectionDistance");
	myDetectionDistance = lua_tonumber( stateTmp, 1);
	lua_pop( stateTmp, 1);
	myAction = NOTHING;

	myQuest = NULL;

	lua_close( stateTmp);
}

void Enemy::oneFrame( Ogre::Real time)
{
	myTimeAction -= time;

	if ( myTimeAction < 0.0f )
	{
		myTimeAction = 0.0f;
	}
	
	if ( myActive )
	{
		if ( myAction == NOTHING )
		{
			myAI->heroDistance( this);
		}

		else if ( myAction == GO_HIT )
		{
			Ogre::Real moveTmp = mySpeed * time;
			myDistance -= moveTmp;

			mySceneNode->translate( myDirection * moveTmp);

			if ( myDistance < 0.0f )
			{	
				myDirection = Ogre::Vector3::ZERO;
				myAction = HIT;
			}
		
			else
			{
				myAI->findDirection( myTarget, this);
			}	
		}

		else if ( myAction == HIT )
		{
			if ( (myTarget->getPosition() - getPosition()).length() > (myTarget->getSize() + getSize()) )
			{
				myAI->findDirection( myTarget, this);
			}

			else if ( myTimeAction <= 0.0f )
			{
				Damage damageTmp;
				damageTmp.damage = 10;
				
				myTarget->receiveDamage( damageTmp);

				myTimeAction = 1.0;
			}
		}
	}
}


void Enemy::activateQuest( Quest* quest)
{
	myQuest = quest;
}